package com.herench.bs.transfer;

import com.herench.bs.transfer.Dialect;
import com.herench.bs.utils.DataFormatUtils;
import org.apache.cxf.endpoint.Client;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * com.herench.bs.transfer.transfer
 *
 * @author wangwenkai
 * @date 2017/10/20/11:39
 * E-mail：605064334@qq.com
 */
public class PayConfirmForAppointmentInsuranceTransfer extends AbstractTransfer {
    public PayConfirmForAppointmentInsuranceTransfer(String method, Client client, String messageMethod) {
        super(method, client,messageMethod);
    }

    @Override
    public String toInvokeMessage(Map<String, Object> params) throws Exception {
        String idCard = (String) params.get("idCard");
        String appointId = (String) params.get("appointId");
        String patientId = (String) params.get("patientId");
        String visitNo = (String) params.get("visitNo");
        String visitDate = DataFormatUtils.getSimpleTimeString((Date) params.get("appointDate"));
        String tranNo = (String) params.get("tranNo");
        String rcptNo = (String) params.get("rcptNo");
        String tradeNo = (String) params.get("tradeNo");
        String orderFee = (String) params.get("orderFee");
        String payType = (String) params.get("payType");
        String moneyPay = (String) params.get("moneyPay");
        //微信21；支付宝20；银联19
        String moneyType ="";
        if("1".equals(moneyPay)){
            moneyType="35";
        }
        if("6".equals(moneyPay)){
            moneyType="20";
        }
        if("2".equals(moneyPay)){
            moneyType="Y";
        }
        //0：自费时传0
        String insurLimits = "0";
        String insurNo = (String) params.get("cardId");
        String insurPwd = "";
        String insurVisitId = (String) params.get("insurVisitId");
        //自费：7
        String insuranceType =  (String) params.get("insuranceType");
        String mtType =  (String) params.get("mtType");
        String insurPay =  (String) params.get("insurPay");
        String accountPay =  (String) params.get("accountPay");
        String ownPay =  (String) params.get("ownPay");
        Document tplDoc = getTplDoc(getMethod());
        Element registElement = tplDoc.getRootElement().element("CommitData").addElement("Regist").addElement("DataRow");
        registElement.addAttribute("PERSON_NO", idCard).addAttribute("PATIENT_ID", patientId).addAttribute("VISIT_NO", visitNo).addAttribute("VISIT_DATE", visitDate).addAttribute("APPOINT_ID", appointId);
        Element billElement = tplDoc.getRootElement().element("CommitData").addElement("Bill").addElement("DataRow");
        billElement.addAttribute("RCPT_NO", rcptNo);
        Element insuranceElement = tplDoc.getRootElement().element("CommitData").addElement("Insurance").addElement("DataRow");
        insuranceElement.addAttribute("INSUR_NO", insurNo).addAttribute("INSUR_PWD", insurPwd).addAttribute("INSUR_VISIT_ID", insurVisitId).addAttribute("INSURANCE_TYPE", insuranceType).addAttribute("PAY_TYPE", payType);
        insuranceElement.addAttribute("MT_TYPE", mtType).addAttribute("INSUR_PAY", insurPay).addAttribute("ACCOUNT_PAY", accountPay).addAttribute("OWN_PAY", ownPay);
        Element paymentElement = tplDoc.getRootElement().element("CommitData").addElement("Payment").addElement("DataRow");
        paymentElement.addAttribute("CARD_NO", idCard).addAttribute("CARD_TYPE", "03").addAttribute("TRAN_NO", tranNo).addAttribute("STREAM_NO", tradeNo).addAttribute("AMOUNT", orderFee).addAttribute("MONEY_TYPE", moneyType);
        return tplDoc.asXML();
    }

    @Override
    public Map<String, Object> toResultMessage(String resMsg) throws Exception {
        Map<String, Object> resMap = new HashMap<>(16);
        Document document = DocumentHelper.parseText(resMsg);
        Element resultInfo = document.getRootElement().element("Result").element("Info");
        String executeStatus = resultInfo.attributeValue("EXECUTE_STATUS");
        String executeMessage = resultInfo.attributeValue("EXECUTE_MESSAGE");
        if (Dialect.getSuccessfulState().equals(executeStatus)) {
            Element dataRow = document.getRootElement().element("ReturnData").element("Data").element("DataRow");
            PayConfirmForAppointmentInnerMessage payConfirmForAppointmentInnerMessage = new PayConfirmForAppointmentInnerMessage(dataRow);
            resMap.put("tranNo", payConfirmForAppointmentInnerMessage.getTranNo());
            resMap.put("streamNo", payConfirmForAppointmentInnerMessage.getStreamNo());
            resMap.put("winNo", payConfirmForAppointmentInnerMessage.getWinNo());
            resMap.put("deptLocation", payConfirmForAppointmentInnerMessage.getDeptLocation());
            resMap.put("serialNo", payConfirmForAppointmentInnerMessage.getSerialNo());
            resMap.put("code", executeStatus);
            resMap.put("message", "成功！");
        } else {
            resMap.put("code", executeStatus);
            resMap.put("message", executeMessage);
        }
        return resMap;
    }


    public class PayConfirmForAppointmentInnerMessage implements InnerMessage {

        /**
         * 合同号
         */
        private String tranNo;
        /**
         * 交易流水号
         */
        private String streamNo;
        /**
         * 窗口号
         */
        private String winNo;
        /**
         * 诊室位置
         */
        private String deptLocation;
        /**
         * 就诊顺序号
         */
        private String serialNo;


        public PayConfirmForAppointmentInnerMessage(Element element) {
            this.tranNo = element.attributeValue("TRAN_NO");
            this.streamNo = element.attributeValue("STREAM_NO");
            this.winNo = element.attributeValue("WIN_NO");
            this.deptLocation = element.attributeValue("DEPT_LOCATION");
            this.serialNo = element.attributeValue("SERIAL_NO");
        }

        public String getTranNo() {
            return tranNo;
        }

        public void setTranNo(String tranNo) {
            this.tranNo = tranNo;
        }

        public String getStreamNo() {
            return streamNo;
        }

        public void setStreamNo(String streamNo) {
            this.streamNo = streamNo;
        }

        public String getWinNo() {
            return winNo;
        }

        public void setWinNo(String winNo) {
            this.winNo = winNo;
        }

        public String getDeptLocation() {
            return deptLocation;
        }

        public void setDeptLocation(String deptLocation) {
            this.deptLocation = deptLocation;
        }

        public String getSerialNo() {
            return serialNo;
        }

        public void setSerialNo(String serialNo) {
            this.serialNo = serialNo;
        }
    }
}
